package com.linxiao.call.controller;

import com.linxiao.call.domain.CrmWebcallFileTel;
import com.linxiao.call.service.ICrmWebcallFileTelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 语音通知批次号码Controller
 * 
 * @author qiqi
 * @date 2022-06-28
 */
@RestController
@RequestMapping("/crm/fileTel")
public class CrmWebcallFileTelController extends BaseController
{
    @Autowired
    private ICrmWebcallFileTelService crmWebcallFileTelService;

    /**
     * 查询语音通知批次号码列表
     */
    @PreAuthorize("@ss.hasPermi('crm:fileTel:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmWebcallFileTel crmWebcallFileTel)
    {
        startPage();
        List<CrmWebcallFileTel> list = crmWebcallFileTelService.selectCrmWebcallFileTelList(crmWebcallFileTel);
        return getDataTable(list);
    }

    /**
     * 导出语音通知批次号码列表
     */
    @PreAuthorize("@ss.hasPermi('crm:fileTel:export')")
    @Log(title = "语音通知批次号码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmWebcallFileTel crmWebcallFileTel)
    {
        List<CrmWebcallFileTel> list = crmWebcallFileTelService.selectCrmWebcallFileTelList(crmWebcallFileTel);
        ExcelUtil<CrmWebcallFileTel> util = new ExcelUtil<CrmWebcallFileTel>(CrmWebcallFileTel.class);
        util.exportExcel(response, list, "语音通知批次号码数据");
    }

    /**
     * 获取语音通知批次号码详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:fileTel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(crmWebcallFileTelService.selectCrmWebcallFileTelById(id));
    }

    /**
     * 新增语音通知批次号码
     */
    @PreAuthorize("@ss.hasPermi('crm:fileTel:add')")
    @Log(title = "语音通知批次号码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmWebcallFileTel crmWebcallFileTel)
    {
        return toAjax(crmWebcallFileTelService.insertCrmWebcallFileTel(crmWebcallFileTel));
    }

    /**
     * 修改语音通知批次号码
     */
    @PreAuthorize("@ss.hasPermi('crm:fileTel:edit')")
    @Log(title = "语音通知批次号码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmWebcallFileTel crmWebcallFileTel)
    {
        return toAjax(crmWebcallFileTelService.updateCrmWebcallFileTel(crmWebcallFileTel));
    }

    /**
     * 删除语音通知批次号码
     */
    @PreAuthorize("@ss.hasPermi('crm:fileTel:remove')")
    @Log(title = "语音通知批次号码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return crmWebcallFileTelService.deleteCrmWebcallFileTelByIds(ids);
    }

    /**
     * 批次号码导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<CrmWebcallFileTel> util = new ExcelUtil<CrmWebcallFileTel>(CrmWebcallFileTel.class);
        util.importTemplateExcel(response, "批次号码");
    }


    @Log(title = "语音通知批次号码", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestPart("file") MultipartFile file, Long fileId) throws Exception {
        if(fileId == null){
            return AjaxResult.error("语音通知批次号码参数【fileId】不能为空！！！");
        }

        ExcelUtil<CrmWebcallFileTel> util = new ExcelUtil<CrmWebcallFileTel>(CrmWebcallFileTel.class);
        List<CrmWebcallFileTel> fileTelList = util.importExcel(file.getInputStream());
        if (StringUtils.isNull(fileTelList) || fileTelList.size() == 0) {
            return AjaxResult.error("导入批次号码数据不能为空！！！");
        }
        AjaxResult result = crmWebcallFileTelService.importFileTel(fileTelList,fileId);
        return result;
    }



}

